package com.ymatou.iapi.godmch.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.setMarketPriceConfigBean;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.setMarketPriceConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.mch_config;
import com.ymttest.database.model.godmch.mch_configExample;
import com.ymttest.database.sqlwapper.godmch.mch_configWapper;
import com.ymttest.utils.TestDataManager;
/**
*设置市场价配置
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_setMarketPriceConfig{
	private static setMarketPriceConfigBean bean;
	private static setMarketPriceConfigCall call;
	private mch_configWapper mchconfigWapper;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置市场价配置");
	}

	@Before
	public void caseUp() {
		bean=new setMarketPriceConfigBean();
		call=new setMarketPriceConfigCall();
		mchconfigWapper=new mch_configWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void check_verify() throws InterruptedException {
		mch_configExample example=new mch_configExample();
        example.createCriteria().andMchIdEqualTo(bean.getMchId());
        mch_config mchconfig=mchconfigWapper.selectByExample(example).get(0);
        
		Logger.verifyEquals("200", call.getStatus1(), "验证status");
		Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
		Logger.verifyEquals(bean.getMarketPriceEnabled(), mchconfig.getMarketPriceEnabled(), "验证数据库中marketPriceEnabled字段的值是否正确");
		Logger.verifyEquals(bean.getMarketPriceTitle()==null?"市场价":bean.getMarketPriceTitle(), mchconfig.getMarketPriceTitle(), "验证数据库中marketPriceTitle字段的值是否正确");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setMarketPriceConfig_001() {
		Logger.start(true,"正例-设置市场价配置-开启-平台商户-config存在,修改-config存在返回成功");
		try {
			
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(3000);
			bean.setMchId(mchId);
			bean.setMarketPriceEnabled(true);
			bean.setMarketPriceTitle("测试市场价标题_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setMarketPriceConfig_004() {
		Logger.start(true,"正例-设置市场价配置-商户有代理的渠道-config不存在,新增-返回成功(设置渠道,代理不变)");
		try {
			//渠道
			Integer userId_channl=GodmchManageCallService.CreateSellerMch();
			//代理
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
	
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("CHN");//设为国内，变为纯代理商
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_agent);
			bean.setMarketPriceEnabled(true);
			bean.setMarketPriceTitle("测试市场价标题_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();	
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setMarketPriceConfig_002() {
		Logger.start(true,"正例-设置市场价配置-独立商户-返回成功");
		try {
			
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
			bean.setMchId(createIndepMchCall.getmchId());
			bean.setMarketPriceEnabled(true);
			bean.setMarketPriceTitle("测试市场价标题_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();	
			
			check_verify();	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setMarketPriceConfig_003() {
		Logger.start(true,"正例-设置市场价配置-一个商户同时设置两次-返回成功");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			bean.setMchId(mchId);
			bean.setMarketPriceEnabled(true);
			bean.setMarketPriceTitle("测试市场价标题_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();			
			call.callService();
			
			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setMarketPriceConfig_005() {
		Logger.start(true,"正例-设置市场价配置-mchId不存在-返回成功");
		try {
			bean.setMchId(TestDataManager.getRandomIntNum(5));
			bean.setMarketPriceEnabled(true);
			bean.setMarketPriceTitle("测试市场价标题_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();			
			call.callService();
			
			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setMarketPriceConfig_006() {
		Logger.start(true,"正例-设置市场价配置-mchId为空-返回成功");
		try {
			//bean.setMchId(TestDataManager.getRandomIntNum(5));
			bean.setMarketPriceEnabled(true);
			bean.setMarketPriceTitle("测试市场价标题_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();			
			call.callService();			
			Logger.verifyEquals("4001", call.getStatus1(), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setMarketPriceConfig_007() {
		Logger.start(true,"正例-设置市场价配置-市场标题为空-返回成功");
		try {
			
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			bean.setMchId(mchId);
			bean.setMarketPriceEnabled(true);
			//bean.setMarketPriceTitle("测试市场价标题_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();			
			
			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setMarketPriceConfig_008() {
		Logger.start(true,"正例-设置市场价配置-开启-平台商户-先开启再关闭-返回成功");
		try {
			
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			bean.setMchId(mchId);
			bean.setMarketPriceEnabled(true);
			bean.setMarketPriceTitle("测试市场价标题_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();			
			
			bean.setMarketPriceEnabled(false);
			bean.setMarketPriceTitle("测试市场价标题_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();	
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}